<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */


    /**
     * Smarty {load_listings} function plugin
     *
     * Type:     function
     * Name:     load_listings
     * Purpose:  Loads most recent listings from the database
     *   @param array
     *   @param Smarty
     *
     * This smarty plugin can be talked to using the following
     * template parameters:
     *   
     *   * var (string)   -- name of variable where to load the 
     *                       data to
     *                       >> default: listings
     *   * toget (string) -- total number of listing to get from
     *                       the database
     *                       >> default: 5
     *
     * @return string
     */
    function smarty_function_load_packages($params, &$smarty)
    {
        global $db; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'articles';
        }else{
            $var = $params['var'];
        }
        
        //do we want to limit to some specific folder structure?
        if(empty($params['folder'])){
            $folder = null;
        }else{
            $node  = $db->getRow("SELECT * FROM structure WHERE id = ".(int)$params['folder']);
            $nodes = $db->getCol("SELECT id FROM structure WHERE lft >= ".(int)$node['lft']." AND rgt <= ".(int)$node['rgt']);
            if(!empty($nodes)){
                $folder = ' AND pm.nodeid IN('.implode(',',$nodes).') ';
            }
        }
                
        //how do I order by?
        if(empty($params['order'])){
            $order = 'pm.published';
        }else{
            $order = $params['order'];
        }
        
        //variable name
        if(empty($params['items'])){
            $total = 5;
        }else{
            $total = (int)$params['items'];
        }
        
        
        if(empty($params['splash'])){
            $splash = null;
        }else{
            $splash = ' AND ni.on_splash = TRUE ';
        }
        
        $pType   = $db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'shoparticlepackage'");
        $items   = $db->getAll("SELECT 
                                        pm.created          AS intime,
                                        pm.title            AS title,
                                        ni.*, 
                                        get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                        'files/pages/".$pType."__'||ni.id||'/' AS image_path
                                    FROM pagemap AS pm 
                                    LEFT JOIN node_shop_package AS ni ON pm.pageid = ni.id
                                    WHERE 
                                    pm.pagetype = " . $pType . "
                                    AND pm.wf_state = 1
                                    ".$splash."
                                    ORDER BY ".$order);
                
        //get sarticle page type
        $pType   = $db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'shoparticle'");
            
        foreach($items as &$item){
            $item['articles'] = $db->getAll("SELECT 
                                                    a.article_id, 
                                                    sa.*,
                                                    title,
                                                    get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                                    'files/pages/".$pType."__'||a.article_id||'/' AS image_path
                                                FROM node_shop_package_article a 
                                                LEFT JOIN pagemap pm ON a.article_id = pm.pageid AND pm.pagetype = ".(int)$pType." 
                                                LEFT JOIN node_shop_article sa ON a.article_id = sa.id
                                                WHERE package_id = ".$item['id']."
                                                ORDER BY title");
            
            //get random image
            $t = $item['articles'];
            shuffle($t);
            $item['rnd'] = array_pop($t);
                            
            //get package pricing
            $item['total_price']   = 0;
            $item['total_price_k'] = 0;
            foreach($item['articles'] as $a){
                if($_SESSION['CART'] and !$_SESSION['CART']->items[$a['article_id']]){
                    $item['total_price'] = $a['price'] + $item['total_price'];
                    $item['total_price_k'] = $a['price_kids'] + $item['total_price_k'];
                }
            }
            
            if(count($item['articles']) > 3){
                $item['discount_price'] = $item['total_price'] * 0.85;
                $item['discount_price_k'] = $item['total_price_k'] * 0.85;
            }elseif(count($item['articles']) > 2){
                $item['discount_price'] = $item['total_price'] * 0.90;
                $item['discount_price_k'] = $item['total_price_k'] * 0.90;
            }elseif(count($item['articles']) > 1){
                $item['discount_price'] = $item['total_price'] * 0.95;
                $item['discount_price_k'] = $item['total_price_k'] * 0.95;
            }else{
                $item['discount_price'] = $item['total_price'];
                $item['discount_price_k'] = $item['total_price_k'];
            }
        }
        
        $smarty->assign($var,$items);
        
        /*
        //pagetype
        $pageType = (int)$db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'shoparticle'");
        
        
        //$filePath = ROOT.'files'.DIRECTORY_SEPARATOR.'pages'.DIRECTORY_SEPARATOR.$pageType.'__'.$this->object.DIRECTORY_SEPARATOR;
        
        //get listings
        if($total == 1){
            $smarty->assign($var,$db->getRow("SELECT 
                                                pm.created          AS intime,
                                                pm.title            AS title,
                                                ni.teaser           AS teaser,
                                                ni.teaser_title     AS teaser_title,
                                                ni.banner_title     AS banner_title,
                                                ni.banner_desc      AS banner_desc,
                                                ni.content          AS content,
                                                ni.image            AS image,
                                                ni.id               AS id,
                                                get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                                'files/pages/".$pageType."__'||ni.id||'/' AS image_path
                                            FROM pagemap AS pm 
                                            LEFT JOIN node_shop_article AS ni ON pm.pageid = ni.id
                                            WHERE pm.pagetype = " . $pageType . " " . $folder . " " . $image . " " . $splash . "
                                            AND pm.wf_state = 1
                                            ORDER BY ".$order." DESC LIMIT ".$total));
        }else{
            $smarty->assign($var,$db->getAll("SELECT 
                                                pm.created          AS intime,
                                                pm.title            AS title,
                                                ni.teaser           AS teaser,
                                                ni.teaser_title     AS teaser_title,
                                                ni.banner_title     AS banner_title,
                                                ni.banner_desc      AS banner_desc,
                                                ni.content          AS content,
                                                ni.image            AS image,
                                                ni.id               AS id,
                                                get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                                'files/pages/".$pageType."__'||ni.id||'/' AS image_path
                                            FROM pagemap AS pm 
                                            LEFT JOIN node_shop_article AS ni ON pm.pageid = ni.id
                                            WHERE pm.pagetype = " . $pageType . " " . $folder . " " . $image . " " . $splash . "
                                            AND pm.wf_state = 1
                                            ORDER BY ".$order." DESC LIMIT ".$total));
        }
        */
    }
?>
